
model = require '../model'

describe 'Model', ()->
  after (done)->
    model.closeDB()
    done()

  User = model.User
  Invite = model.Invite
  Attachment = model.Attachment
  Tag = model.Tag
  Post = model.Post
  Tag_Post = model.Tag_Post
  rdn = (min=10000)-> Math.random()*min|0
  it 'User setter should worker', (done)->
    #debugger
    tnickname = 'DReampuf' + rdn()
    u = new User
      nickname: tnickname
      password: 'XXXX'
      gender: 0
      email: "soddyque#{rdn()}@gmail.com"
    u.isValid (valid)->
      valid.should.equal true
      u.nickname.should.equal tnickname

      u.save (err, u_saved)->
        User.all
          where:
            nickname: u.nickname
          , (err, res)->
            res.length.should.equal 1
            for i in res
              i.nickname_low.should.equal u.nickname.toLowerCase()
              i.password.length.should.equal 64
              i.gender.should.equal 0

            done()
  
  it 'Invite type convert auto', (done)->
    icode = rdn()
    i = new Invite
      code: icode
    i.isValid (valid)->
      valid.should.equal true
      i.done.should.equal 0
      i.save (err, _)->
        Invite.all
          where:
            code: icode
          , (err, cds)->
            cds.length.should.equal 1
            for i in cds
              i.code.should.equal icode + ''
              i.done.should.equal 0

            done()

  it 'User and Invite link', (done)->
    icode = rdn()
    i = new Invite
      code: icode
    i.isValid (valid)->
      i.save (err, _)->
        Invite.all
          where:
            code: icode
          , (err, cds)->
            cds.length.should.equal 1
            iInvite = cds[0]
            tnickname = 'DReampuf' + rdn()
            u = new User
              nickname: tnickname
              password: 'XXXX'
              gender: 0
              email: "soddyque#{rdn()}@gmail.com"
            u.isValid (valid)->
              valid.should.equal true
              u.save (err, u_saved)->
                User.all
                  where:
                    nickname: u.nickname
                  , (err, res)->
                    res.length.should.equal 1
                    iUser = res[0]
                    #iUser.invite_code = iInvite
                    iInvite.user = iUser.id
                    iInvite.save (_, _t)->
                      _t.user.should.equal iUser.id | 0
                      done()


  it 'User and attachs', (done)->
    u = new User
      nickname: 'DrEamput' + rdn()
      password: 'XXXX'
      gender: 0
      email: "soddyque#{rdn()}@gmail.com"
    u.save validate: true, (err, u)->
      Attachment.count (err, acount)->
        rcount = rd = rdn(5) + 1
        u.attachs (err, attachs)->
          u_destroy_num = attachs.length
          #console.log "old attachs.length", attachs.length
          #console.log "u.attachs", attachs, attachs.length
          #u_attach_destroy = ()->
          #  if --u_destroy_num > 0
          #    attachs[u_destroy_num].destroy ()->
          #      u_attach_destroy()
          #  else
          #    add_Attach()

          add_Attach = ()->
            a = new Attachment
              uri: "http://tucao.in/#{ rdn() }.txt"
            a.user u
            a.save validate:true, (err, a)->
              console.log err if err
              if --rd
                add_Attach()
              else
                u.attachs (err, atts)->
                  #console.log atts.length
                  atts.length.should.equal u_destroy_num+rcount
                  done()
                  
                #Attachment.all
                #  where:
                #    user_attach: u.id
                #, (err, new_acount)->
                #  console.log "new_acount", new_acount, acount, rcount
                #  new_acount.length.should.equal acount + rcount
                #  u.attachs (err, attachs)->
                #    console.log "attachs.length", attachs.length
                #    #attachs.length.should.equal rcount

                #    done()
          #u_attach_destroy()
          add_Attach()

  it 'Posts & Tags', (done)->
    u = new User
      nickname: 'DrEamput' + rdn()
      password: 'XXXX'
      gender: 0
      email: "soddyque#{rdn()}@gmail.com"
    u.save validate: true, (err, u)->
      console.log err if err
      t = new Tag
        name:"User" + rdn()
      t.save validate:true, (err, t)->
        console.log err if err
        add_p = (num)->
          if num < 1
            return done()
          p = new Post
            content: "sssasd ![asdasdd](sadsadfsdaf) **asdasd** \n # 112312sdfsad\n asdgsddgdgg"
            author_name: u.nickname
            ip: "127.0.1.1"
          p.tags t
          p.save validate: true, (err, p)->
            t.posts p
            t.save validate: true, (err, t)->
              console.log err if err
              add_p(--num)
        add_p(4)
